package com.binest.projectInclusion.service.impl;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;

import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.binest.common.constant.HttpStatus;
import com.binest.common.core.page.TableDataInfo;
import com.binest.common.utils.DateUtils;
import com.binest.draftBox.mapper.DraftBoxMapper;
import com.binest.foreignInvest.domain.ForeignInvest;
import com.binest.foreignInvest.mapper.ForeignInvestMapper;
import com.binest.foreignInvest.service.IForeignInvestService;
import com.binest.inclusionExamine.mapper.InclusionExamineMapper;
import com.binest.inclusionExamine.service.IInclusionExamineService;
import com.binest.keyEvent.domain.KeyEvent;
import com.binest.keyEvent.mapper.KeyEventMapper;
import com.binest.keyEvent.service.IKeyEventService;
import com.binest.officeInformation.domain.OfficeInformation;
import com.binest.officeInformation.service.IOfficeInformationService;
import com.binest.projectInclusion.domain.ProjectInclusion;
import com.binest.projectInclusion.mapper.ProjectInclusionMapper;
import com.binest.projectInclusion.service.IProjectInclusionService;
import com.binest.projectInclusionMain.mapper.ProjectInclusionMainMapper;
import com.binest.projectInvestDetails.domain.ProjectInvestDetails;
import com.binest.projectInvestDetails.mapper.ProjectInvestDetailsMapper;
import com.binest.projectInvestDetails.service.IProjectInvestDetailsService;
import com.binest.socialMediaConnection.domain.SocialMediaConnection;
import com.binest.socialMediaConnection.mapper.SocialMediaConnectionMapper;
import com.binest.socialMediaConnection.service.ISocialMediaConnectionService;
import com.binest.workExperience.domain.WorkExperience;
import com.binest.workExperience.service.IWorkExperienceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



/**
 * 项目收录Service业务层处理
 *
 * @author wwd
 * @date 2024-01-24
 */
@Service
public class ProjectInclusionServiceImpl implements IProjectInclusionService
{
    @Autowired
    private ProjectInclusionMapper projectInclusionMapper;
    @Autowired
    private IOfficeInformationService officeInformationService;
    @Autowired
    private SocialMediaConnectionMapper socialMediaConnectionMapper;

    @Autowired
    private KeyEventMapper keyEventMapper;
    @Autowired
    private ProjectInvestDetailsMapper projectInvestDetailsMapper;

    @Autowired
    private ProjectInclusionMainMapper projectInclusionMainMapper;
    @Autowired
    private DraftBoxMapper draftBoxMapper;

    @Autowired
    private InclusionExamineMapper inclusionExamineMapper;

    @Autowired
    private ForeignInvestMapper foreignInvestMapper;


    @Autowired
    private ElasticsearchClient client;



    @Autowired
    private IInclusionExamineService inclusionExamineService;

    @Autowired
    private ISocialMediaConnectionService socialMediaConnectionService;
    @Autowired
    private IKeyEventService keyEventService;
    @Autowired
    private IForeignInvestService foreignInvestService;
    @Autowired
    private IProjectInvestDetailsService projectInvestDetailsService;
    @Autowired
    private IWorkExperienceService workExperienceService;
    /**
     * 查询项目收录
     *
     * @param id 项目收录主键
     * @return 项目收录
     */
    @Override
    public ProjectInclusion selectProjectInclusionById(String id)
    {
        ProjectInclusion res =projectInclusionMapper.selectProjectInclusionById(id);
        List<SocialMediaConnection> smcList=socialMediaConnectionService.selectSocialMediaConnectionByInId(id);
        if(smcList!=null){
            res.setSmcList(smcList);
        }
        //关键事件
        List<KeyEvent> eventList =keyEventService.selectKeyEventListByInId(id);
        if(eventList!=null)
        {
            res.setEventList(eventList);
        }
        List<WorkExperience> workExperienceList =workExperienceService.selectWorkExperienceByInId(id);
        if(workExperienceList!=null)
        {
            res.setWorkExperienceList(workExperienceList);
        }
        // 对外投资
         List<ForeignInvest> foreignInvestList=foreignInvestService.selectForeignInvestListByInId(id);
        if(foreignInvestList!=null)
        {
            res.setForeignInvestList(foreignInvestList);
        }
        // 项目投资详情
         List<ProjectInvestDetails> projectInvestDetailsList=projectInvestDetailsService.selectProjectInvestDetailsListByInId(id);
        if(projectInvestDetailsList!=null)
        {
            res.setProjectInvestDetailsList(projectInvestDetailsList);
        }
        return res;
    }

    @Override
    public ProjectInclusion selectEsProjectInclusionById(String id) throws Exception {
        ProjectInclusion res = new ProjectInclusion();
        try {
            // 执行查询
            SearchResponse<ProjectInclusion> searchResponse = client.search(srBuilder -> srBuilder
                            .index("project_inclusion")
                            // 多个id查询
                            .query(queryBuilder -> queryBuilder
                                    .ids(idsQueryBuilder -> idsQueryBuilder
                                            .values(id))
                            )
                    , ProjectInclusion.class);

            //解析查询结果
            List<Hit<ProjectInclusion>> hitList = searchResponse.hits().hits();
            res = hitList.get(0).source();

        } catch (IOException e) {
            throw new Exception("查询ES数据失败");

        }


        return res;
    }

    @Override
    public ProjectInclusion selectProjectInclusionByMainId(String mainId, String lang) {
        ProjectInclusion projectInclusion=new ProjectInclusion();
        projectInclusion.setMainId(mainId);
        projectInclusion.setLang(lang);
        return projectInclusionMapper.selectProjectInclusionByMainId(projectInclusion);
    }

    /**
     * 查询项目收录列表
     *
     * @param projectInclusion 项目收录
     * @return 项目收录
     */
    @Override
    public List<ProjectInclusion> selectProjectInclusionList(ProjectInclusion projectInclusion)

    {
        List<ProjectInclusion> resList=projectInclusionMapper.selectProjectInclusionList(projectInclusion);
        for(ProjectInclusion  p:resList){
           String   exStatus= inclusionExamineService.getExamineStatus(p.getId());
            if(exStatus!=null){
                p.setExStatus(exStatus);
            }else{
                p.setExStatus("");
            }

            OfficeInformation  officeInformation=   officeInformationService.selectOfficeInformationById(p.getAuditFirms());
            if(officeInformation!=null){
                p.setAuditFirms(officeInformation.getName());
            }else{
                p.setAuditFirms("other Audit Firms");
            }
        }


        return resList;
    }

    @Override
    public TableDataInfo selectProjectInclusionEsList(ProjectInclusion projectInclusion) throws Exception {
        TableDataInfo rspData = new TableDataInfo();
        // 将多个查询条件放入list中.
        List<Query> queries = new ArrayList<>();
        List<Query> querie = new ArrayList<>();
        List<Query> locationQuery  = new ArrayList<>();
        List<Query> InvestDetaiQuery  = new ArrayList<>();
        //最新 最热
        String type = "";
        if (projectInclusion.getType().equals("0")) {
            //最新
            type = "createTime.keyword";
        } else {
            type = "likeCount.keyword";
        }
        // 升序降序
        String esSort = "";

        if (projectInclusion.getSort().equals("0")) {
            //最新
            esSort = "Desc";
        } else {
            esSort = "Asc";
        }

        //判断中英文  0英文  1中文
        Query byLang = TermQuery.of(a -> a
                .field("lang")
                .value(projectInclusion.getLang())
        )._toQuery();

        querie.add(byLang);
         //标签
        if (projectInclusion.getLabel() != null) {
            // 将字符串转换成集合
            List<String> aList = Arrays.asList(projectInclusion.getLabel().split(","));
            for (String label : aList) {
                Query labels = MatchQuery.of(m -> m
                        .query(label)
                        .field("label")
                        .fuzziness("AUTO")
                )._toQuery();
                queries.add(labels);
            }
            Query bool1 =  BoolQuery.of(m -> m
                    .should(queries)
            )._toQuery();
            querie.add(bool1);
        }
        //发币情况
        if (projectInclusion.getCoinIssuanceSatus() != null) {
                Query ecologys = TermQuery.of(m -> m
                        .field("coinIssuanceSatus")
                        .value(projectInclusion.getCoinIssuanceSatus())
                )._toQuery();
            querie.add(ecologys);
        }
        //地区
        if (projectInclusion.getLocation() != null) {
            // 将字符串转换成集合
            List<String> cList = Arrays.asList(projectInclusion.getLocation().split(","));
            for (String location : cList) {
                Query locations = MatchQuery.of(m -> m
                        .query(location)
                        .field("location")
                        .fuzziness("AUTO")
                )._toQuery();
                locationQuery.add(locations);
            }

            Query bool2 =  BoolQuery.of(m -> m
                    .should(locationQuery)
            )._toQuery();
            querie.add(bool2);

        }
        //轮次   融资状态
        if (projectInclusion.getFinancingStatus() != null) {
            // 将字符串转换成集合
            List<String> dList = Arrays.asList(projectInclusion.getFinancingStatus().split(","));
            for (String financingStatus : dList) {
                Query financingStatuss = MatchQuery.of(m -> m
                        .query(financingStatus)
                        .field("projectInvestDetailsList.round")
                        .fuzziness("AUTO")
                )._toQuery();
                InvestDetaiQuery.add(financingStatuss);
            }
            Query bool3 =  BoolQuery.of(m -> m
                    .should(InvestDetaiQuery)
            )._toQuery();
            querie.add(bool3);

        }
        try {
            // 从es中根据条件查询结果.
            String finalType = type;
            String finalEsSort = esSort;
            SearchResponse<ProjectInclusion> searchResponse = client.search(s -> s
                            .index("project_inclusion")
                            .query(q -> q
                                    .bool(t -> t
                                            .must(querie)
                                    )

                            )

                            .from((projectInclusion.getPageNum() - 1) * projectInclusion.getPageSize())
                            .size(projectInclusion.getPageSize())
                            .sort(sort -> sort.field(f -> f.field(finalType).order(SortOrder.valueOf(finalEsSort))))
                    , ProjectInclusion.class);
            List<Hit<ProjectInclusion>> hitList = searchResponse.hits().hits();

            List<ProjectInclusion> list = new ArrayList<>();
            for (Hit<ProjectInclusion> mapHit : hitList) {
                list.add(mapHit.source());
            }

            long total = searchResponse.hits().total() != null ? searchResponse.hits().total().value() : 0;

            rspData.setCode(HttpStatus.SUCCESS);
            rspData.setMsg("查询成功");
            rspData.setRows(list);
            rspData.setTotal(total);

            return rspData;
        } catch (Exception e) {
            throw new Exception("【查询 -> 失败】从es中组合查询出错，错误信息为：{}");
        }finally {
            if(rspData.getRows()==null){
                List<ProjectInclusion> list = new ArrayList<>();
                rspData.setRows(list);
                rspData.setTotal(0);
                rspData.setCode(HttpStatus.SUCCESS);
                rspData.setMsg("查询成功");
            }
            return rspData;
        }

    }


    @Override
    public TableDataInfo selectProjectInclusionRecommendEsList(ProjectInclusion projectInclusion) throws Exception {
        TableDataInfo rspData = new TableDataInfo();
        // 将多个查询条件放入list中.
        List<Query> queries = new ArrayList<>();
        List<Query> querie = new ArrayList<>();
        List<Query> locationQuery  = new ArrayList<>();
        List<Query> InvestDetaiQuery  = new ArrayList<>();
        //最新 最热
        String type = "";

            type = "createTime.keyword";

        String esSort = "";


            //最新
            esSort = "Desc";

        //判断中英文  0英文  1中文
        Query byLang = TermQuery.of(a -> a
                .field("lang")
                .value(projectInclusion.getLang())
        )._toQuery();

        querie.add(byLang);
        //标签
        if (projectInclusion.getLabel() != null) {
            // 将字符串转换成集合
            List<String> aList = Arrays.asList(projectInclusion.getLabel().split(","));
            for (String label : aList) {
                Query labels = MatchQuery.of(m -> m
                        .query(label)
                        .field("label")
                        .fuzziness("AUTO")
                )._toQuery();
                queries.add(labels);
            }
            Query bool1 =  BoolQuery.of(m -> m
                    .should(queries)
            )._toQuery();
            querie.add(bool1);
        }

        Query names = TermQuery.of(a -> a
                .field("name")
                .value(projectInclusion.getName())
        )._toQuery();
        queries.add(names);
        try {
            // 从es中根据条件查询结果.
            String finalType = type;
            String finalEsSort = esSort;
            SearchResponse<ProjectInclusion> searchResponse = client.search(s -> s
                            .index("project_inclusion")
                            .query(q -> q
                                    .bool(t -> t
                                            .must(querie)
                                            .mustNot(queries)
                                    )
                            )

                            .from(0)
                            .size(10)
                            .sort(sort -> sort.field(f -> f.field(finalType).order(SortOrder.valueOf(finalEsSort))))
                    , ProjectInclusion.class);
            List<Hit<ProjectInclusion>> hitList = searchResponse.hits().hits();

            List<ProjectInclusion> list = new ArrayList<>();
            for (Hit<ProjectInclusion> mapHit : hitList) {
                list.add(mapHit.source());
            }

            long total = searchResponse.hits().total() != null ? searchResponse.hits().total().value() : 0;

            rspData.setCode(HttpStatus.SUCCESS);
            rspData.setMsg("查询成功");
            rspData.setRows(list);
            rspData.setTotal(total);

            return rspData;
        } catch (Exception e) {
            throw new Exception("【查询 -> 失败】从es中组合查询出错，错误信息为：{}");
        }finally {
            if(rspData.getRows()==null){
                List<ProjectInclusion> list = new ArrayList<>();
                rspData.setRows(list);
                rspData.setTotal(0);
                rspData.setCode(HttpStatus.SUCCESS);
                rspData.setMsg("查询成功");
            }
            return rspData;
        }

    }


    /**
     * 新增项目收录
     *
     * @param projectInclusion 项目收录
     * @return 结果
     */
    @Override
    public String insertProjectInclusion(ProjectInclusion projectInclusion)
    {
        projectInclusion.setCreateTime(DateUtils.getNowDate());

//
//        //判断是否有主表id
//        if (projectInclusion.getMainId() == null) {
//
//            String uuid = IdUtils.simpleUUID();
//            String mainId = IdUtils.simpleUUID();
//            projectInclusion.setId(uuid);
//            projectInclusion.setMainId(mainId);
//            projectInclusion.setCreateTime(DateUtils.getNowDate());
//            projectInclusion.setCreateBy((String) StpUtil.getLoginId());
//
//            //新增社交媒体链接
//            List<SocialMediaConnection> smcList=new ArrayList<>();
//            if (projectInclusion.getSmcList() != null) {
//
//                for (SocialMediaConnection s : projectInclusion.getSmcList()) {
//                    s.setId(IdUtils.simpleUUID());
//                    //收录id
//                    s.setInclusionId(uuid);
//                    s.setCreateBy((String)StpUtil.getLoginId());
//                    s.setCreateTime(DateUtils.getNowDate());
//                    smcList.add(s);
//                    socialMediaConnectionMapper.insertSocialMediaConnection(s);
//                }
//            }
//            projectInclusion.setSmcList(smcList);
//            //关键事件
//            List<KeyEvent> eventList=new ArrayList<>();
//            if (projectInclusion.getEventList() != null) {
//
//                for (KeyEvent w : projectInclusion.getEventList()) {
//                  //收录id
//                    w.setInclusionId(uuid);
//                    w.setId(IdUtils.simpleUUID());
//                    w.setCreateBy((String)StpUtil.getLoginId());
//                    w.setCreateTime(DateUtils.getNowDate());
//                    eventList.add(w);
//                    keyEventMapper.insertKeyEvent(w);
//                }
//            }
//            projectInclusion.setEventList(eventList);
//            //对外投资
//            List<ForeignInvest> foreignInvestList=new ArrayList<>();
//            if (projectInclusion.getForeignInvestList() != null) {
//                for (ForeignInvest f : projectInclusion.getForeignInvestList()) {
//                    //收录id
//                    f.setInclusionId(uuid);
//                    f.setId(IdUtils.simpleUUID());
//                    f.setCreateBy((String)StpUtil.getLoginId());
//                    f.setCreateTime(DateUtils.getNowDate());
//                    foreignInvestList.add(f);
//                    foreignInvestMapper.insertForeignInvest(f);
//                }
//            }
//            projectInclusion.setForeignInvestList(foreignInvestList);
//            //投资详情
//            List<ProjectInvestDetails> projectInvestDetailsList =new ArrayList<>();
//            if (projectInclusion.getProjectInvestDetailsList() != null) {
//                for (ProjectInvestDetails i : projectInclusion.getProjectInvestDetailsList()) {
//                    i.setId(IdUtils.simpleUUID());
//                    //收录id
//                    i.setInclusionId(uuid);
//                    i.setCreateBy((String)StpUtil.getLoginId());
//                    i.setCreateTime(DateUtils.getNowDate());
//                    projectInvestDetailsList.add(i);
//                    projectInvestDetailsMapper.insertProjectInvestDetails(i);
//                }
//            }
//            projectInclusion.setProjectInvestDetailsList(projectInvestDetailsList);
//            //新增项目收录
//            projectInclusionMapper.insertProjectInclusion(projectInclusion);
//            //新增项目收录主表
//            ProjectInclusionMain projectInclusionMain = new ProjectInclusionMain();
//            projectInclusionMain.setId(mainId);
//            projectInclusionMain.setCreateTime(DateUtils.getNowDate());
//            projectInclusionMain.setCreateBy((String)StpUtil.getLoginId());
//            projectInclusionMainMapper.insertProjectInclusionMain(projectInclusionMain);
//
//            //判断存草稿箱 0还是提交收录 1
//            if (projectInclusion.getSubmitType().equals("0")) {
//
//                DraftBox draftBox = new DraftBox();
//                draftBox.setId(IdUtils.simpleUUID());
//                //项目名称
//                draftBox.setName(projectInclusion.getName());
//                //类型  项目
//                draftBox.setType("0");
//                draftBox.setProjectId(uuid);
//                draftBox.setUserId((String)StpUtil.getLoginId());
//                draftBox.setCreateBy((String)StpUtil.getLoginId());
//                draftBox.setCreateTime(DateUtils.getNowDate());
//                draftBox.setLang(projectInclusion.getLang());
//                draftBoxMapper.insertDraftBox(draftBox);
//            } else {
//
//                InclusionExamine inclusionExamine = new InclusionExamine();
//
//                inclusionExamine.setId(IdUtils.simpleUUID());
//                //未审核
//                inclusionExamine.setStatus("0");
//                inclusionExamine.setName(projectInclusion.getName());
//                //类型 项目
//                inclusionExamine.setType("0");
//                inclusionExamine.setProjectId(uuid);
//                inclusionExamine.setUserId((String)StpUtil.getLoginId());
//                inclusionExamine.setCreateBy((String)StpUtil.getLoginId());
//                inclusionExamine.setCreateTime(DateUtils.getNowDate());
//                inclusionExamine.setLang(projectInclusion.getLang());
//
//                //上传ipfs
//                JSONObject jsonObject = JSONObject.fromObject(projectInclusion);
//                byte[] jsonData = jsonObject.toString().getBytes();
//                try {
//                    String qmHash= ipfsService.uploadToIpfs(jsonData);
//                    inclusionExamine.setQmHash(qmHash);
//                } catch (IOException e) {
//                    throw new RuntimeException("上传ipfs错误");
//                }
//                inclusionExamineMapper.insertInclusionExamine(inclusionExamine);
//            }
//
//        } else {
//            String uuid = IdUtils.simpleUUID();
//            projectInclusion.setId(uuid);
//            projectInclusion.setCreateTime(DateUtils.getNowDate());
//            projectInclusion.setCreateBy((String)StpUtil.getLoginId());
//
//            //新增项目
//            projectInclusionMapper.insertProjectInclusion(projectInclusion);
//            //新增社交媒体链接
//            List<SocialMediaConnection> smcList=new ArrayList<>();
//            if (projectInclusion.getSmcList() != null) {
//
//                for (SocialMediaConnection s : projectInclusion.getSmcList()) {
//                    s.setId(IdUtils.simpleUUID());
//                    //收录id
//                    s.setInclusionId(uuid);
//                    s.setCreateBy((String)StpUtil.getLoginId());
//                    s.setCreateTime(DateUtils.getNowDate());
//                    smcList.add(s);
//                    socialMediaConnectionMapper.insertSocialMediaConnection(s);
//                }
//            }
//            projectInclusion.setSmcList(smcList);
//            //关键事件
//            List<KeyEvent> eventList=new ArrayList<>();
//            if (projectInclusion.getEventList() != null) {
//
//                for (KeyEvent w : projectInclusion.getEventList()) {
//                    //收录id
//                    w.setInclusionId(uuid);
//                    w.setId(IdUtils.simpleUUID());
//                    w.setCreateBy((String)StpUtil.getLoginId());
//                    w.setCreateTime(DateUtils.getNowDate());
//                    eventList.add(w);
//                    keyEventMapper.insertKeyEvent(w);
//                }
//            }
//            projectInclusion.setEventList(eventList);
//            //对外投资
//            List<ForeignInvest> foreignInvestList=new ArrayList<>();
//            if (projectInclusion.getForeignInvestList() != null) {
//                for (ForeignInvest f : projectInclusion.getForeignInvestList()) {
//                    //收录id
//                    f.setInclusionId(uuid);
//                    f.setId(IdUtils.simpleUUID());
//                    f.setCreateBy((String)StpUtil.getLoginId());
//                    f.setCreateTime(DateUtils.getNowDate());
//                    foreignInvestList.add(f);
//                    foreignInvestMapper.insertForeignInvest(f);
//                }
//            }
//            projectInclusion.setForeignInvestList(foreignInvestList);
//            //投资详情
//            List<ProjectInvestDetails> projectInvestDetailsList =new ArrayList<>();
//            if (projectInclusion.getProjectInvestDetailsList() != null) {
//                for (ProjectInvestDetails i : projectInclusion.getProjectInvestDetailsList()) {
//                    i.setId(IdUtils.simpleUUID());
//                    //收录id
//                    i.setInclusionId(uuid);
//                    i.setCreateBy((String)StpUtil.getLoginId());
//                    i.setCreateTime(DateUtils.getNowDate());
//                    projectInvestDetailsList.add(i);
//                    projectInvestDetailsMapper.insertProjectInvestDetails(i);
//                }
//            }
//
//            if (projectInclusion.getSubmitType().equals("0")) {
//
//                DraftBox draftBox = new DraftBox();
//                draftBox.setId(IdUtils.simpleUUID());
//                //项目 名称
//                draftBox.setName(projectInclusion.getName());
//                //类型  项目
//                draftBox.setType("0");
//                draftBox.setProjectId(uuid);
//                draftBox.setUserId((String)StpUtil.getLoginId());
//                draftBox.setCreateBy((String)StpUtil.getLoginId());
//                draftBox.setCreateTime(DateUtils.getNowDate());
//                draftBox.setLang(projectInclusion.getLang());
//                draftBoxMapper.insertDraftBox(draftBox);
//            } else {
//
//                InclusionExamine inclusionExamine = new InclusionExamine();
//
//                inclusionExamine.setId(IdUtils.simpleUUID());
//                //未审核
//                inclusionExamine.setStatus("0");
//                inclusionExamine.setName(projectInclusion.getName());
//                //类型 项目
//                inclusionExamine.setType("0");
//                inclusionExamine.setProjectId(uuid);
//                inclusionExamine.setUserId((String)StpUtil.getLoginId());
//                inclusionExamine.setCreateBy((String)StpUtil.getLoginId());
//                inclusionExamine.setCreateTime(DateUtils.getNowDate());
//                inclusionExamine.setLang(projectInclusion.getLang());
//                //上传ipfs
//                JSONObject jsonObject = JSONObject.fromObject(projectInclusion);
//                byte[] jsonData = jsonObject.toString().getBytes();
//                try {
//                    String qmHash= ipfsService.uploadToIpfs(jsonData);
//                    inclusionExamine.setQmHash(qmHash);
//                } catch (IOException e) {
//                    throw new RuntimeException("上传ipfs错误");
//                }
//                inclusionExamineMapper.insertInclusionExamine(inclusionExamine);
//
//            }
//
//        }
        return projectInclusion.getMainId();
    }

    @Override
    public String insertNewProjectInclusion(ProjectInclusion projectInclusion) {

        //先判断是否有人已经提交审核 能否继续更新

        ProjectInclusion newProjectInclusion=new ProjectInclusion();
//        newProjectInclusion.setMainId(projectInclusion.getMainId());
//        List<ProjectInclusion>  resList=  projectInclusionMapper.selectProjectInclusionList(newProjectInclusion);
//        for(int a=0;a<resList.size();a++){
//            InclusionExamine inclusionExamine =new InclusionExamine();
//            inclusionExamine.setProjectId(resList.get(a).getId());
//            //待审核
//            inclusionExamine.setStatus("0");
//            List<InclusionExamine>  inclusionList=  inclusionExamineService.selectInclusionExamineList(inclusionExamine);
//
//            if(inclusionList.size()>0){
//                return  "该收录存在待审核状态，无法更新";
//            }
//        }
//
//            String uuid = IdUtils.simpleUUID();
//            projectInclusion.setId(uuid);
//            projectInclusion.setUpdateTime(DateUtils.getNowDate());
//            projectInclusion.setUpdateBy((String)StpUtil.getLoginId());
//
//            //新增项目
//            projectInclusionMapper.insertProjectInclusion(projectInclusion);
//            //新增社交媒体链接
//        //新增社交媒体链接
//        List<SocialMediaConnection> smcList=new ArrayList<>();
//        if (projectInclusion.getSmcList() != null) {
//
//            for (SocialMediaConnection s : projectInclusion.getSmcList()) {
//                s.setId(IdUtils.simpleUUID());
//                //收录id
//                s.setInclusionId(uuid);
//                s.setCreateBy((String)StpUtil.getLoginId());
//                s.setCreateTime(DateUtils.getNowDate());
//                smcList.add(s);
//                socialMediaConnectionMapper.insertSocialMediaConnection(s);
//            }
//        }
//        projectInclusion.setSmcList(smcList);
//        //关键事件
//        List<KeyEvent> eventList=new ArrayList<>();
//        if (projectInclusion.getEventList() != null) {
//
//            for (KeyEvent w : projectInclusion.getEventList()) {
//                //收录id
//                w.setInclusionId(uuid);
//                w.setId(IdUtils.simpleUUID());
//                w.setCreateBy((String)StpUtil.getLoginId());
//                w.setCreateTime(DateUtils.getNowDate());
//                eventList.add(w);
//                keyEventMapper.insertKeyEvent(w);
//            }
//        }
//        projectInclusion.setEventList(eventList);
//        //对外投资
//        List<ForeignInvest> foreignInvestList=new ArrayList<>();
//        if (projectInclusion.getForeignInvestList() != null) {
//            for (ForeignInvest f : projectInclusion.getForeignInvestList()) {
//                //收录id
//                f.setInclusionId(uuid);
//                f.setId(IdUtils.simpleUUID());
//                f.setCreateBy((String)StpUtil.getLoginId());
//                f.setCreateTime(DateUtils.getNowDate());
//                foreignInvestList.add(f);
//                foreignInvestMapper.insertForeignInvest(f);
//            }
//        }
//        projectInclusion.setForeignInvestList(foreignInvestList);
//        //投资详情
//        List<ProjectInvestDetails> projectInvestDetailsList =new ArrayList<>();
//        if (projectInclusion.getProjectInvestDetailsList() != null) {
//            for (ProjectInvestDetails i : projectInclusion.getProjectInvestDetailsList()) {
//                i.setId(IdUtils.simpleUUID());
//                //收录id
//                i.setInclusionId(uuid);
//                i.setCreateBy((String)StpUtil.getLoginId());
//                i.setCreateTime(DateUtils.getNowDate());
//                projectInvestDetailsList.add(i);
//                projectInvestDetailsMapper.insertProjectInvestDetails(i);
//            }
//        }
//
//
//        if (projectInclusion.getSubmitType().equals("0")) {
//
//                DraftBox draftBox = new DraftBox();
//                draftBox.setId(IdUtils.simpleUUID());
//                //项目 名称
//                draftBox.setName(projectInclusion.getName());
//                //类型  项目
//                draftBox.setType("0");
//                draftBox.setProjectId(uuid);
//                draftBox.setUserId((String)StpUtil.getLoginId());
//                draftBox.setUpdateBy((String)StpUtil.getLoginId());
//                draftBox.setUpdateTime(DateUtils.getNowDate());
//                draftBox.setLang(projectInclusion.getLang());
//                draftBoxMapper.insertDraftBox(draftBox);
//            } else {
//
//                InclusionExamine inclusionExamine = new InclusionExamine();
//
//                inclusionExamine.setId(IdUtils.simpleUUID());
//                //未审核
//                inclusionExamine.setStatus("0");
//                inclusionExamine.setName(projectInclusion.getName());
//                //类型 项目
//                inclusionExamine.setType("0");
//                inclusionExamine.setProjectId(uuid);
//                inclusionExamine.setUserId((String)StpUtil.getLoginId());
//                inclusionExamine.setUpdateBy((String)StpUtil.getLoginId());
//                inclusionExamine.setUpdateTime(DateUtils.getNowDate());
//                inclusionExamine.setLang(projectInclusion.getLang());
//            //上传ipfs
//            JSONObject jsonObject = JSONObject.fromObject(projectInclusion);
//            byte[] jsonData = jsonObject.toString().getBytes();
//            try {
//                String qmHash= ipfsService.uploadToIpfs(jsonData);
//                inclusionExamine.setQmHash(qmHash);
//            } catch (IOException e) {
//                throw new RuntimeException("上传ipfs错误");
//            }
//                inclusionExamineMapper.insertInclusionExamine(inclusionExamine);
//
//            }


        return projectInclusion.getMainId();
    }

    /**
     * 修改项目收录
     *
     * @param projectInclusion 项目收录
     * @return 结果
     */
    @Override
    public int updateProjectInclusion(ProjectInclusion projectInclusion)

    {

//        projectInclusion.setUpdateBy((String)StpUtil.getLoginId());
//        projectInclusion.setUpdateTime(DateUtils.getNowDate());
//
//            //新增社交媒体链接
//            if (projectInclusion.getSmcList() != null) {
//                for (SocialMediaConnection s : projectInclusion.getSmcList()) {
//                    s.setUpdateBy((String)StpUtil.getLoginId());
//                    s.setUpdateTime(DateUtils.getNowDate());
//                    socialMediaConnectionMapper.updateSocialMediaConnection(s);
//                }
//            }
//            //关键事件
//            if (projectInclusion.getEventList() != null) {
//                for (KeyEvent w : projectInclusion.getEventList()) {
//                    w.setUpdateBy((String)StpUtil.getLoginId());
//                    w.setUpdateTime(DateUtils.getNowDate());
//                    keyEventMapper.updateKeyEvent(w);
//                }
//            }
//
//            //对外投资
//            if (projectInclusion.getForeignInvestList() != null) {
//                for (ForeignInvest f : projectInclusion.getForeignInvestList()) {
//                    f.setUpdateBy((String)StpUtil.getLoginId());
//                    f.setUpdateTime(DateUtils.getNowDate());
//                    foreignInvestMapper.updateForeignInvest(f);
//                }
//            }
//            //投资详情
//            if (projectInclusion.getProjectInvestDetailsList() != null) {
//                for (ProjectInvestDetails i : projectInclusion.getProjectInvestDetailsList()) {
//                    i.setCreateBy((String)StpUtil.getLoginId());
//                    i.setCreateTime(DateUtils.getNowDate());
//                    projectInvestDetailsMapper.updateProjectInvestDetails(i);
//                }
//            }
//            //判断存草稿箱 0还是提交收录 1
//            if (projectInclusion.getSubmitType().equals("0")) {
//                //通过项目id 查询当前草稿箱内的是否有这条数据
//                DraftBox  draftBox= draftBoxMapper.selectDraftBoxByProId(projectInclusion.getId());
//                    draftBox.setName(projectInclusion.getName());
//                    draftBox.setUpdateBy((String)StpUtil.getLoginId());
//                    draftBox.setUpdateTime(DateUtils.getNowDate());
//                     draftBox.setLang(projectInclusion.getLang());
//                    draftBoxMapper.updateDraftBox(draftBox);
//            }

        return projectInclusionMapper.updateProjectInclusion(projectInclusion);
    }

    /**
     * 批量删除项目收录
     *
     * @param ids 需要删除的项目收录主键
     * @return 结果
     */
    @Override
    public int deleteProjectInclusionByIds(String[] ids)
    {
        return projectInclusionMapper.deleteProjectInclusionByIds(ids);
    }

    /**
     * 删除项目收录信息
     *
     * @param id 项目收录主键
     * @return 结果
     */
    @Override
    public int deleteProjectInclusionById(String id)
    {
        return projectInclusionMapper.deleteProjectInclusionById(id);
    }
}
